package com.sdkx.app.preference;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;

import com.larswerkman.holocolorpicker.ColorPicker;
import com.larswerkman.holocolorpicker.OpacityBar;
import com.larswerkman.holocolorpicker.SVBar;
import com.sdkx.app.R;


public class ColorPickerPreference implements Preference.OnPreferenceClickListener {
    private Context context;

    public ColorPickerPreference(Context context){
        this.context = context;
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("选择颜色");
        View view = LayoutInflater.from(context).inflate(R.layout.color_picker,null);
        final ColorPicker picker = (ColorPicker)view.findViewById(R.id.picker);
        final SVBar svBar = (SVBar)view.findViewById(R.id.svbar);
        final OpacityBar opacityBar = (OpacityBar) view.findViewById(R.id.opacitybar);
        picker.addSVBar(svBar);
        picker.addOpacityBar(opacityBar);
        picker.setShowOldCenterColor(false);
        int oldColor = sharedPreferences.getInt("track_point_color",-1);
        if(oldColor!=-1)
        picker.setColor(oldColor);
        builder.setView(view);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                int argb = picker.getColor();

                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("track_point_color",argb);
                editor.commit();
            }
        });
        Dialog dialog = builder.create();
        dialog.show();
        return false;
    }
}
